package drds.server.util;

import java.io.File;
import java.io.IOException;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

public class LogBackConfigLoader {

	public static void load(String configFilePath) throws IOException, JoranException {
		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

		File configFile = new File(configFilePath);
		if (!configFile.exists()) {
			throw new IOException("Logback External Config File Parameter does not reference a file that exists");
		} else {
			if (!configFile.isFile()) {
				throw new IOException("Logback External Config File Parameter exists, but does not reference a file");
			} else {
				if (!configFile.canRead()) {
					throw new IOException("Logback External Config File exists and is a file, but cannot be read.");
				} else {
					JoranConfigurator joranConfigurator = new JoranConfigurator();
					joranConfigurator.setContext(loggerContext);
					loggerContext.reset();
					joranConfigurator.doConfigure(configFilePath);
					StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
				}
			}
		}
	}

}